# face detector 5
# face_recognition

import face_recognition
from matplotlib import pyplot as plt
import cv2


# visualize functions
def show_img_with_matplotlib(color_img, title, pos):
    img_rgb = color_img[:, :, ::-1]
    plt.subplot(1, 2, pos)
    plt.imshow(img_rgb)
    plt.title(title, fontsize=8)
    plt.axis('off')


img = cv2.imread('picture/004.jpg')
rgb_img = img[:, :, ::-1]
# load image
# face_locations() 的第一个参数是输入图像 (RGB)；
# 第二个参数设置在执行检测之前对输入图像上采样的次数；第三个参数是要使用的人脸检测模型。
# 采用HOG模型进行人脸检测
rects_1 = face_recognition.face_locations(rgb_img, 0, "hog")
rects_2 = face_recognition.face_locations(rgb_img, 1, "hog")
print(rects_1)
print(rects_2)


# draw face detector box
def show_detection(image, faces):
    for face in faces:
        top, right, bottom, left = face
        cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0,), 10)
    return image


# display test result
img_faces_1 = show_detection(img.copy(), rects_1)
img_faces_2 = show_detection(img.copy(), rects_2)
show_img_with_matplotlib(img_faces_1, "face_locations(rgb,0,hog):" + str(len(rects_1)), 1)
show_img_with_matplotlib(img_faces_2, "face_locations(rgb,1,hog):" + str(len(rects_2)), 2)

plt.show()
